﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
using Common;
using System.Linq;

namespace TaxiDispatchClient
{
    public partial class OrderListForm : DevExpress.XtraEditors.XtraForm
    {
        public OrderListForm()
        {
            InitializeComponent();
        }

        private DataTable _dtOrderList = new DataTable();
      
        private void OrderListForm_Load(object sender, EventArgs e)
        {
            _dtOrderList.Columns.Add("Id");
            _dtOrderList.Columns.Add("PassengerName");
            _dtOrderList.Columns.Add("PhoneNumber");
            _dtOrderList.Columns.Add("PhoneOrderId");
            _dtOrderList.Columns.Add("StartAddress");
            _dtOrderList.Columns.Add("EndAddress");
            _dtOrderList.Columns.Add("StartLat");
            _dtOrderList.Columns.Add("StartLng");
            _dtOrderList.Columns.Add("EndLat");
            _dtOrderList.Columns.Add("EndLng");
            _dtOrderList.Columns.Add("DriverId");
            _dtOrderList.Columns.Add("CarId");
            _dtOrderList.Columns.Add("CallTime", typeof(DateTime));
            _dtOrderList.Columns.Add("TakeTime", typeof(DateTime));
            _dtOrderList.Columns.Add("SubmitTime", typeof(DateTime));
            _dtOrderList.Columns.Add("PsgerAcceptTime", typeof(DateTime));
            _dtOrderList.Columns.Add("DriverAcceptTime", typeof(DateTime));
            _dtOrderList.Columns.Add("FinishTime", typeof(DateTime));
            _dtOrderList.Columns.Add("AgreeStartTime", typeof(DateTime));
            _dtOrderList.Columns.Add("AgreeEndTime", typeof(DateTime));
            _dtOrderList.Columns.Add("Price");
            _dtOrderList.Columns.Add("State");
            _dtOrderList.Columns.Add("Remark");
            _dtOrderList.Columns.Add("UserId");
            _dtOrderList.Columns.Add("UserRealName");
            _dtOrderList.Columns.Add("DriverName");       
            _dtOrderList.Columns.Add("CarNo");
             
            RefreshData();
        }

        private void RefreshData()
        {
            _dtOrderList.Clear();
            List<OrderInfo> orderlist = new List<OrderInfo>();
            lock (MainForm.OrderList.SyncRoot)
            {
                orderlist = MainForm.OrderList.Values.Cast<OrderInfo>().ToList();
            }
            int notHandleCount = 0;
            foreach (OrderInfo orderInfo in orderlist)
            {
                DataRow row = _dtOrderList.NewRow();
                row["Id"] = orderInfo.Id;
                row["PhoneNumber"] = orderInfo.PhoneNumber;
                row["PassengerName"] = orderInfo.PassengerName;
                row["PhoneOrderId"] = orderInfo.PhoneOrderId;
                row["StartAddress"] = orderInfo.StartAddress;
                row["EndAddress"] = orderInfo.EndAddress;
                row["StartLng"] = orderInfo.StartLng;
                row["StartLat"] = orderInfo.StartLat;
                row["EndLng"] = orderInfo.EndLng;
                row["EndLat"] = orderInfo.EndLat;
                row["DriverId"] = orderInfo.DriverId;
                row["CarId"] = orderInfo.CarId;
                row["CallTime"] = orderInfo.CallTime;
                row["TakeTime"] = orderInfo.TakeTime;
                row["SubmitTime"] = orderInfo.SubmitTime;                
                row["PsgerAcceptTime"] = orderInfo.PsgerAcceptTime;              
                row["DriverAcceptTime"] = orderInfo.DriverAcceptTime;                 
                row["FinishTime"] = orderInfo.FinishTime;                 
                row["AgreeStartTime"] = orderInfo.AgreeStartTime;
                row["AgreeEndTime"] = orderInfo.AgreeEndTime;                 
                row["Price"] = orderInfo.Price;
                row["State"] = CommonRoutine.GetOrderStateString(orderInfo.State);
                row["Remark"] = orderInfo.Remark;
                row["UserId"] = orderInfo.UserId;
                row["UserRealName"] = orderInfo.UserRealName;
                row["DriverName"] = orderInfo.DriverName;              
                row["CarNo"] = orderInfo.CarNo;
                 
                if (orderInfo.State == OrderState.S2_TakenByPlatform)
                {
                    notHandleCount++;
                }
                _dtOrderList.Rows.Add(row);
            }
            DataView dv = _dtOrderList.DefaultView;
            dv.Sort = "id desc";
            _dtOrderList = dv.ToTable();
            
            gridControl1.DataSource = _dtOrderList;
            gridView1.GroupPanelText = "共" + orderlist.Count 
                + "条订单，其中有"+notHandleCount+ "条尚未处理";
            btn_refresh.Enabled = true;
            barBtnItem_refresh.Enabled = true;
        }
 

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                return;
            }
            DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (row == null)
            {
                return;
            }
            #region 显示菜单 并根据订单状态设置菜单选项是否可用
            OrderState orderState = CommonRoutine.GetOrderState(row["State"].ToString());
            switch (orderState)
            {
                case OrderState.S0_Cancled:
                    barBtnItem_cancleOrder.Enabled = false;
                    barBtnItem_handleOrder.Enabled = false;
                    barBtnItem_finishOrder.Enabled = true;
                    break;
                case OrderState.S1_NewOrder:
                    barBtnItem_cancleOrder.Enabled = true;
                    barBtnItem_handleOrder.Enabled = true;
                    barBtnItem_finishOrder.Enabled = false;
                    break;
                case OrderState.S2_TakenByPlatform:
                    barBtnItem_cancleOrder.Enabled = true;
                    barBtnItem_handleOrder.Enabled = true;
                    barBtnItem_finishOrder.Enabled = false;
                    break;
                case OrderState.S3_SetCarAndDriver:
                    barBtnItem_cancleOrder.Enabled = true;
                    barBtnItem_handleOrder.Enabled = false;
                    barBtnItem_finishOrder.Enabled = false;
                    break;
                case OrderState.S4_SentInfo:
                    barBtnItem_cancleOrder.Enabled = true;
                    barBtnItem_handleOrder.Enabled = false;
                    barBtnItem_finishOrder.Enabled = false;
                    break;
                case OrderState.S5_AcceptByDriver:
                    barBtnItem_cancleOrder.Enabled = true;
                    barBtnItem_handleOrder.Enabled = false;
                    barBtnItem_finishOrder.Enabled = true;
                    break;
                case OrderState.S6_Finish:
                    barBtnItem_cancleOrder.Enabled = false;
                    barBtnItem_handleOrder.Enabled = false;
                    barBtnItem_finishOrder.Enabled = true;
                    break;
                case OrderState.S9_Unknown:
                    barBtnItem_cancleOrder.Enabled = false;
                    barBtnItem_handleOrder.Enabled = false;
                    barBtnItem_finishOrder.Enabled = false;
                    break;                    
            }
            popupMenu_right.ShowPopup(Cursor.Position);
            #endregion
        }

        private void barBtnItem_handleOrder_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (row != null)
            {
                OrderInfo order = new OrderInfo();
                order.Id = int.Parse(row["Id"].ToString());
                order.PhoneNumber = row["PhoneNumber"].ToString();
                order.PassengerName = row["PassengerName"].ToString();
                order.PhoneOrderId = row["PhoneOrderId"].ToString();
                order.StartAddress = row["StartAddress"].ToString();
                order.EndAddress = row["EndAddress"].ToString();
                order.StartLng = double.Parse(row["StartLng"].ToString());
                order.StartLat = double.Parse(row["StartLat"].ToString());
                order.EndLng = double.Parse(row["EndLng"].ToString());
                order.EndLat = double.Parse(row["EndLat"].ToString());
                order.DriverId = int.Parse(row["DriverId"].ToString());
                order.CarId = int.Parse(row["CarId"].ToString());
                order.CallTime = DateTime.Parse(row["CallTime"].ToString());
                order.TakeTime = DateTime.Parse(row["TakeTime"].ToString());
                order.SubmitTime = DateTime.Parse(row["SubmitTime"].ToString());
                order.PsgerAcceptTime = DateTime.Parse(row["PsgerAcceptTime"].ToString());
                order.DriverAcceptTime = DateTime.Parse(row["DriverAcceptTime"].ToString());
                order.FinishTime = DateTime.Parse(row["FinishTime"].ToString());
                order.AgreeStartTime = DateTime.Parse(row["AgreeStartTime"].ToString());
                order.AgreeEndTime = DateTime.Parse(row["AgreeEndTime"].ToString());
                order.Price = ushort.Parse(row["Price"].ToString());
                order.State = CommonRoutine.GetOrderState(row["State"].ToString());
                order.Remark = row["Remark"].ToString();
                order.UserId = int.Parse(row["UserId"].ToString());
                order.UserRealName = row["UserRealName"].ToString();
                order.DriverName = row["DriverName"].ToString();
                order.CarNo = row["CarNo"].ToString();
                AddOrderForm form = new AddOrderForm(order);
                form.ShowDialog();
            }
        }

        private void barBtnItem_refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_refresh.Enabled = false;
            barBtnItem_refresh.Enabled = false;
            RefreshData();
        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            btn_refresh.Enabled = false;
            barBtnItem_refresh.Enabled = false;
            RefreshData();
        }

        private void barBtnItem_cancleOrder_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {            
            DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (row != null)
            {
                if (XtraMessageBox.Show("是否确定删除该订单？", "提醒：",
                MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
                OrderInfo order = new OrderInfo();
                order.Id = int.Parse(row["Id"].ToString());
            }

        }

        

    }
}